//
//  AFNetworkHandle.m
//  UI22_项目框架
//
//  Created by dllo on 15/7/3.
//  Copyright (c) 2015年 蓝天. All rights reserved.
//

#import "AFNetworkHandle.h"


@implementation AFNetworkHandle

+ (void)GetwithAFNByUrl:(NSString *)urlstr completion:(void (^)(id))block
{
    // 1. 网址转码
    NSString *str = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // 2. 创建AFN网络请求管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 3. 设置响应解析对象
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 4. 设置数据支持类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain", nil]];
    // 5. AFN Get请求
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 请求成功执行区域
        // responseObject 返回数据为NSData
        if (responseObject) {
            // 如果返回数据不为空开始Json解析
            id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            block(result);
        }
        else
        {
            NSLog(@"返回数据为空, 请检查");
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 请求失败执行区域
        NSLog(@"%@", error);
    }];
    
    
    
}

@end
